package controller

import (
	"github.com/gin-gonic/gin"
	"shop-bean/model"
	"shop-bean/request"
	"shop-common/library/serializer"
	"shop-common/utils/contextArgs"
	"shop-common/utils/queryWrapper"
	"shop-service/business"
)

type AreaController struct {
}

// @Tags 省市区
// @description: 获取三级联动
// @Summary 获取三级联动
// @Security ApiKeyAuth
// @function: List
// @Accept  json
// @Produce  json
// @Success 200 {object} string "{"code":0," result":{}, "message":"操作成功", "time":1618493448}"
// @Router /admin/area/list [get]
func (a *AreaController) List(ctx *gin.Context) {
	areas, err := business.ServiceImpl().AreaService.Find(ctx, nil)
	if err != nil {
		serializer.Fail(ctx, serializer.PublicQueryErr)
		return
	}
	serializer.SuccessData(ctx, "", &areas)
}

// @Tags 省市区
// @description: 通过父级id获取区域列表
// @Summary 通过父级id获取区域列表
// @Security ApiKeyAuth
// @function: ListByPid
// @Accept  json
// @Produce  json
// @Param pid query int true "父id" default(0)
// @Success 200 {object} string "{"code":0," result":{}, "message":"操作成功", "time":1618493448}"
// @Router /admin/area/listByPid [get]
func (a *AreaController) ListByPid(ctx *gin.Context) {
	pid, err := contextArgs.GetUriIntQuery(ctx, "pid")
	if err != nil {
		serializer.Fail(ctx, err.Error())
		return
	}
	areas, err := business.ServiceImpl().AreaService.Find(ctx, queryWrapper.Eq("parent_id", pid))
	if err != nil {
		serializer.Fail(ctx, serializer.PublicQueryErr)
		return
	}

	serializer.SuccessData(ctx, "", areas)
}

// @Tags 省市区
// @description: 新增三级联动
// @Summary 新增三级联动
// @Security ApiKeyAuth
// @function: Save
// @Accept  json
// @Produce  json
// @Param data body request.AreaRequest true "请求参数"
// @Success 200 {object} string "{"code":0," result":{}, "message":"操作成功", "time":1618493448}"
// @Router /admin/area [post]
func (a *AreaController) Save(ctx *gin.Context) {
	form := request.AreaRequest{}
	if err := serializer.Verify(ctx, &form); err != nil {
		serializer.Fail(ctx, err.Error())
		return
	}

	area := model.Area{
		AreaName: form.AreaName,
		ParentId: form.ParentId,
		Level:    form.Level,
	}

	err := business.ServiceImpl().AreaService.Create(ctx, &area)
	if err != nil {
		serializer.Fail(ctx, serializer.PublicCreateErr)
		return
	}

	serializer.Success(ctx, "")
}

// @Tags 省市区
// @description: 修改三级联动
// @Summary 修改三级联动
// @Security ApiKeyAuth
// @function: Update
// @Accept  json
// @Produce  json
// @Param data body request.AreaRequest true "请求参数"
// @Success 200 {object} string "{"code":0," result":{}, "message":"操作成功", "time":1618493448}"
// @Router /admin/area [put]
func (a *AreaController) Update(ctx *gin.Context) {
	form := request.AreaRequest{}
	if err := serializer.Verify(ctx, &form); err != nil {
		serializer.Fail(ctx, err.Error())
		return
	}

	if form.AreaId < 1 {
		serializer.Fail(ctx, "地区ID不符合规范")
		return
	}

	columns := map[string]interface{}{
		"area_name": form.AreaName,
		"level":     form.Level,
	}

	if form.ParentId > 0 {
		columns["parent_id"] = form.ParentId
	}

	err := business.ServiceImpl().AreaService.Updates(ctx, form.AreaId, columns)
	if err != nil {
		serializer.Fail(ctx, serializer.PublicUpdateErr)
		return
	}

	serializer.Success(ctx, "")
}

// @Tags 省市区
// @description: 删除
// @Summary 删除
// @Security ApiKeyAuth
// @function: Delete
// @Accept  json
// @Produce  json
// @Param areaId path string true "areaId"
// @Success 200 {object} string "{"code":0," result":{}, "message":"操作成功", "time":1618493448}"
// @Router /admin/area/{areaId} [delete]
func (a *AreaController) Delete(ctx *gin.Context) {
	areaId, err := contextArgs.GetUriIntPath(ctx, "areaId")
	if err != nil {
		serializer.Fail(ctx, err.Error())
		return
	}

	err = business.ServiceImpl().AreaService.Delete(ctx, uint(areaId))
	if err != nil {
		serializer.Fail(ctx, serializer.PublicDeleteErr)
		return
	}

	serializer.Success(ctx, "")
}
